<?php
namespace app\admin\controller;
use think\Request;
class Hunanconstr extends Base
{
    // 首页框架
    public function index()
    {
        if(! session('?user_id')) {
            $this->redirect('admin/Hunanconstr/login');
        }
        
        return $this->fetch();
    }
    
    //批量轮播图上传
    public function List_img_uploads($name,$sk,$request)
    {
        $path = 'public' . DS;
        $paths = 'uploads' . DS . $sk . DS . 'admin' . '/';
        $img['0']=request()->file($name.'1');
        $img['1']=request()->file($name.'2');
        $img['2']=request()->file($name.'3');
        foreach ($img as $key=>$file) {
            if(!empty($file)){
                $info = $file
                ->move(ROOT_PATH . $path . $paths);
                if ($info) {
                    $imgs[$key] = $paths . $info->getSaveName();
                } else {
                    err('', $file->getError());
                }
            }else{
                $imgs[$key]="";
            }
        }
        //返回数据
        $mg = json_encode($imgs);
        return $mg;
    }
    
    ##分解轮播
    public function resolve_imgs($images,$new_image)
    {
        $images= json_decode($images);      //查询数据库有图片的情况下，转换成json格式
        $new_image=json_decode($new_image);  //把新图片替换成旧图片
        //图片替换操作
        if(!empty($images)){
            foreach ($new_image as $key=>$value) {
                //判断图片不为空的情况下修改
                if($new_image[$key]!=null && $new_image!=""){
                    $images[$key]=$new_image[$key];
                }else{//保持原有图片格式
                }
            }
        }else{
            $images=$new_image; //如果图片为空的话就直接用新图片替换成旧图片
        }
    
        $images=json_encode($images);
        return $images;
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {
            $post = request()->only(['account', 'pwd'], 'post');
             
            if(empty($post['account']) or empty($post['pwd'])) {
                err('', '账号或密码错误');
            }
            
            $result = db('customer')->where(['username' => $post['account'],'id'=>1000])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('', '账号与密码不匹配');
            }
            
            if($result['status'] != 1) {
                err('', '账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('', '账号与密码不匹配');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('', '验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 验证码检测
    public function check($code = '')
    {
        $captcha = new \think\captcha\Captcha();
        if(! $captcha->check($code)) {
            $this->error('验证码错误,请从新输入....');
        }
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/Hunanconstr/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
    
    /**
     * 员工管理
     * **/
    //员工列表
    public function employee_list(){
        $res = db('wx_jg_personal')->order('create_time desc')->paginate($this->page,false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //员工删除
    public function employee_del($id)
    {
        $res = db('wx_jg_personal')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/employee_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/employee_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //用户列表
    public function UserList()
    {
        $where['admin_id'] = $this->accountId;
        $res = db('applet_user')->where($where)->paginate($this->page,false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //用户删除
    public function UserDel($id)
    {
        $res = db('applet_user')->where(['id'=>$id,'admin_id'=>$this->accountId])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/UserList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/UserList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    /**
     * 企业管理
     * **/
    //公司列表
    public function company_list()
    {
        $res = db('company')->alias('a')->join('wx_jg_binding b','a.code = b.code','left')
                ->field('a.id,a.company_name,a.company_logo,a.code,a.create_time,
                        a.update_time,a.status,a.sort,a.state,b.name,a.payment,a.amount_all,a.item_number')
                ->order('a.sort asc')
                ->paginate($this->page,false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //公司增加
    public function company_add()
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            $data['company_name'] = $post['company_name'];
            $data['status'] = $post['status'];
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            if (!empty($img)){
            $data['company_logo'] = IMG_ADMIN.$img;
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['sort'] = $post['sort'];
            $data['code'] = $post['code'];
            $res = db('company')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/company_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/company_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
            
        }
        return $this->fetch();
    }
    
    //公司编辑
    public function company_edit($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            $data['company_name'] = $post['company_name'];
            $data['status'] = $post['status'];
            
            
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            if (!empty($img)){
            $data['company_logo'] = IMG_ADMIN.$img;
            }
            $data['update_time'] = time();
            $data['sort'] = $post['sort'];
            $data['code'] = $post['code'];
            $data['item_number'] = $post['item_number'];
            $data['amount_all'] = $post['amount_all'];
            $data['payment'] = $post['payment'];
            $res = db('company')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/company_list',['id'=>$id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/company_list',['id'=>$id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
            
        }
        $where['id'] = $id;
        $res = db('company')->where($where)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //公司删除
    public function company_del($id,$type)
    {
        $res = db('company')->where(['id'=>$id])->update(['state'=>$type]);
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = url('admin/Hunanconstr/company_list',['id'=>$id]);
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = url('admin/Hunanconstr/company_list',['id'=>$id]);
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    /**
     * 工程管理
     * **/
    //项目列表
    public function project_list()
    {
        $where['status'] = ['neq',-1];
        $res = db('project')->where($where)->order('create_time asc')->paginate($this->page,false);
        foreach ($res as $k => $v){
            $smp = db('wx_jg_lagging')->field('countent')->where(['item_id'=>$v['id']])->find();
            $v['countent'] = $smp['countent'];
            $res->offsetSet($k,$v);
        }
        //获取统计数量
        $arr = db('project')
               ->field('count(*) as number,sum(plan_output_total) as plan_output_total,sum(total_area) as total_area')
               ->where($where)->where(['review_status'=>1,'status'=>1])
               ->find();
        //已开工项目
        $arra = db('project')
                ->field('count(*) as number')
                ->where(['review_status'=>1,'start_work'=>1,'pact_status'=>1,'status'=>1])
                ->find();
        //正常的项目
        $arraa = db('project')
                ->field('count(*) as number')
                ->where(['project_status'=>['in','1,2']])
                ->where('review_status=1 and status = 1')
                ->find();
        //滞后项目
        $str = db('project')
            ->field('count(*) as number')
            ->where("project_status=0")
            ->where('review_status=1 and status = 1')
            ->find();
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('arr', $arr);
        $this->assign('arra', $arra);
        $this->assign('arraa', $arraa);
        $this->assign('str', $str);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //项目审核
    public function examine($id,$type)
    {
        $res = db('project')->where(['id'=>$id])->update(['review_status'=>$type]);
        if ($res AND $type == 1){
            //如果审核通过，创建绑定账户
            $arr = db('project')->field('add_uid,code')->where(['id'=>$id])->find();
            //获取个人信息
            $name = db('applet_user')->field('avatarurl,nickname,mobile')->where(['id'=>$arr['add_uid']])->find();
            $data = [];
            $data['user_id'] = $arr['add_uid'];
            $data['item_id'] = $id;
            $data['position'] = "普通员工";
            $data['code'] = $arr['code'];
            $data['head_img'] = $name['avatarurl'];
            $data['name'] = $name['nickname'];
            $data['tel'] = $name['mobile'];
            $data['create_time'] = time();
            $str = db('wx_jg_bound')->insert($data);
        }elseif ($res AND $type == 2){
            //如果审核通过，创建绑定账户
            $arr = db('project')->field('add_uid,code')->where(['id'=>$id])->find();
            //获取个人信息
            $name = db('applet_user')->field('avatarurl,nickname,mobile')->where(['id'=>$arr['add_uid']])->find();
            $str = db('wx_jg_bound')->where(['user_id'=>$arr['add_uid'],'item_id'=>$id])->delete();
        }
    if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //项目增加
    public function project_add()
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //判断用户ID是否存在
            $add_uid = db('applet_user')->where(['admin_id'=>1000,'id'=>$post['add_uid']])->find();
            if (!$add_uid){
                $data = [];
                $data['status'] = 0;
                $data['info'] = '创建用户ID不存在';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }else{
                $data['add_uid'] = $post['add_uid'];
            }
            
            //判断用户编号是否为空
            $add_uid = db('project')->where(['project_no'=>$post['project_no']])->find();
            if ($add_uid){
                $data = [];
                $data['status'] = 0;
                $data['info'] = '项目编号存在了';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
            else{
                $data['project_no'] = $post['project_no'];
            }
            
            //判断公司ID是否为空
            $company_id = db('company')->where(['id'=>$post['company_id']])->find();
            if (!$company_id){
                $data = [];
                $data['status'] = 0;
                $data['info'] = '公司ID不存在';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
            else{
                $data['company_id'] = $post['company_id'];
                $data['item_name'] = $company_id['company_name'];
            }
            //随机生产邀请码
            $code = rand(0,99999);
            //判断生邀请码是否存在
            $code_num=true;
            while ($code_num){
                $arr = db('project')->where(['code'=>$code])->find();
                if(empty($arr)){
                    $code_num=false;
                }
            }
            $data['code'] = 88888;
            $data['project_name'] = $post['project_name'];
            $data['plan_output_total'] = $post['plan_output_total'];
            $data['total_area'] = $post['total_area'];
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['can_count'] = $post['can_count'];
            $data['project_status'] = $post['project_status'];
            //$data['review_status'] = $post['review_status'];
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            if (!empty($img)){
                $data['project_model'] = IMG_ADMIN.$img;
            }
            $res = db('project')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        //公司名称
        $res = db('company')->field('id,company_name')->where("status = 1 and state = 1")->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //项目状态编辑
    public function project_edit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $company_id= db('company')->field('id')->where(['company_name'=>$post['item_name']])->find();
            $data['company_id'] = $company_id['id'];
            $data['project_name'] = $post['project_name'];
            $data['project_no'] = $post['project_no'];
            $data['item_name'] = $post['item_name'];
            $data['construction_unit'] = $post['item_name'];
            $data['plan_finish_time'] = $post['plan_finish_time'];
            $data['project_status'] = $post['project_status'];
            $data['pact_status'] = $post['pact_status'];
            $data['start_work'] = $post['start_work'];
            $data['update_time'] = time();
            $res = db('project')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        $res = db('project')
                ->where(['id'=>$id])->find();
        //项目
        $mst = db('wx_jg_event')->where(['item_id'=>$id,'event_name'=>'开工日期'])->find();
        if ($mst and $mst['event_time'] != '请选择日期'){
            $res['event_time'] = $mst['event_time'];
        }else {
            $res['event_time'] = '';
        }
        $arr = db('area')->where(['id'=>$res['province_id']])->find();
        $str = db('area')->where(['id'=>$res['city_id']])->find();
        $res['province_id'] = $arr['name'];
        $res['city_id'] = $str['name'];
        
        //大事记
        $ress = db('wx_jg_event')
        ->where("item_id=$id")
        ->order('sort asc')
        ->order('create_time asc')
        ->select();
        $this->assign('ress', $ress);
       
        //公司名称
        $rest = db('company')->field('id,company_name')->where("status = 1 and state = 1")->select();
        $this->assign('rest',$rest);
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //项目删除
    public function project_del($id)
    {
        $res = db('project')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //项目筛选
    public function ProjectSearch()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $where['a.company_name|b.project_name'] = ['like','%'.$post['search'].'%'];
            $res = db('company')->alias('a')->join('project b','a.id = b.company_id')
                                    ->where($where)
                                    ->select();
            //获取统计数量
            $where = [];
            $where['b.company_name|a.project_name'] = ['like','%'.$post['search'].'%'];
            $arr = db('project')->alias('a')->join('company b','b.id = a.company_id','left')
            ->field('count(a.id) as number,sum(a.plan_output_total) as plan_output_total,sum(a.total_area) as total_area')
            ->where($where)->where(['a.review_status'=>1])
            ->find();
                     
            $this->assign('res',$res);
            $this->assign('arr',$arr);
            return $this->fetch();
        }
    }
    
    //项目详情列表
    public function project_cat_list($id)
    {
        //项目信息
        $res = db('project')->where(['id'=>$id])->find();
        
        if (empty($res['engin_time'])){
            $res['engin_time'] = time();
        }
        
        //获取开工时间
        $mst = db('wx_jg_event')->where(['item_id'=>$id,'event_name'=>'开工日期'])->find();
        if ($mst and $mst['event_time'] != '请选择日期'){
            $res['event_time'] = $mst['event_time'];
        }else {
            $res['event_time'] = '待定';
        }
        //城市
        $arrs = db('area')->where(['id'=>$res['province_id']])->find();
        $str = db('area')->where(['id'=>$res['city_id']])->find();
        $res['province_id'] = $arrs['name'];
        $res['city_id'] = $str['name'];
        //成员信息
        $arr = db('wx_jg_bound')->where(['item_id'=>$id])->select();
        //云相册
        $img = db('wx_jg_album')->where(['item_id'=>$id])->select();
        foreach ($img as $k => $v){
            $img[$k]['image'] = json_decode($v['image']);
        }
        
        $this->assign('img',$img);
        $this->assign('arr',$arr);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //项目详情-项目修改
    public function project_cat_edit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            //获取省份id
            if (!empty($post['province_id'])){
                $province_id = db('area')->field('id')->where(['name'=>$post['province_id']])->find();
                $data['province_id'] = $province_id['id'];
            }
            //获取城市id
            if (!empty($post['city_id'])){
                $city_id = db('area')->field('id')->where(['name'=>$post['city_id']])->find();
                $data['city_id'] = $city_id['id'];
            }
            $data['address'] = $post['address'];
            $data['construction_unit'] = $post['construction_unit'];
            $data['design_unit'] = $post['design_unit'];
            $data['contract_unit'] = $post['contract_unit'];
            $data['supervisor_unit'] = $post['supervisor_unit'];
            $data['total_area'] = $post['total_area'];
            $data['finished_output'] = $post['finished_output'];
            $data['finished_area'] = $post['finished_area'];
            $data['recovery_money'] = $post['recovery_money'];
            $data['update_time'] = time();
            $data['lat'] = $post['lat'];
            $data['lng'] = $post['lng'];

            $res = db('project')->where(['id'=>$id])->update($data);

   
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        //项目信息
        $res = db('project')->where(['id'=>$id])->find();
        $mst = db('wx_jg_event')->where(['item_id'=>$id,'event_name'=>'开工日期'])->find();
        if ($mst and $mst['event_time'] != '请选择日期'){
            $res['event_time'] = $mst['event_time'];
        }else {
            $res['event_time'] = '';
        }
        $arr = db('area')->where(['id'=>$res['province_id']])->find();
        $str = db('area')->where(['id'=>$res['city_id']])->find();
        $res['province_id'] = $arr['name'];
        $res['city_id'] = $str['name'];
        $this->assign('res',$res);
        return $this->fetch('project_edit');
    }

   
    //成员信息
    public function contacts($id)
    {
        //dump($id);die;
        $res = db('wx_jg_bound')->alias('a')->join('applet_user b','a.user_id = b.id')
                ->field('a.id,a.head_img,a.name,a.tel,a.position,b.country,b.province,b.city')
                ->where(['a.item_id'=>$id,'b.admin_id'=>$this->accountId])
                ->order('a.type asc')
                ->select();
        //dump($res);die;
        $this->assign('res',$res);
        $this->assign('user_id',$id);
        return $this->fetch();
    }
    
    //成员删除
    public function user_del($id,$user_id)
    {
        $res = db('wx_jg_bound')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //成员编辑
    public function user_edit($id,$user_id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            if (!empty($post['position_name'])){
                //获取职位信息
                $arr = db('wx_jg_framework')->where(['position_name'=>$post['position_name']])->find();
                $data['position'] = $post['position_name'];
                $data['position_id'] = $arr['id'];
                $data['type'] = $arr['type'];
            }
            $res = db('wx_jg_bound')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        $res = db('wx_jg_bound')->where(['id'=>$id])->find();
        //职位
        $position = db('wx_jg_framework')->select();
        $this->assign('position',$position);
        $this->assign('res',$res);
        return $this->fetch();
    }

    //绑定用户
    public function BundEdit($user_id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            //判断是否已经绑定过
            $all = db('wx_jg_bound')->where(['user_id'=>$post['id'],'item_id'=>$user_id])->find();
            
            if ($all){
                $data = [];
                $data['status'] = 0;
                $data['info'] = '当前用户已经绑定过此项目';
                $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
            
            //获取绑定用户信息
            $res = db('applet_user')->field('avatarurl,nickname')
                             ->where(['id'=>$post['id']])
                             ->find();
            
            //获取职位信息
            $arr = db('wx_jg_framework')->where(['position_name'=>$post['position_name']])->find();
            
            $data['item_id'] = $user_id;
            $data['position_id'] = $arr['id'];
            $data['position'] = $arr['position_name'];
            $data['type'] = $arr['type'];
            $data['sort'] = $arr['sort'];
            $data['user_id'] = $post['id'];
            $data['head_img'] = $res['avatarurl'];
            $data['name'] = $res['nickname'];
            $data['code'] = 88888;
            $data['create_time'] = time();

            $str = db('wx_jg_bound')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/contacts',['id'=>$user_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        //获取所有职位
        $res = db('wx_jg_framework')->select();
        $this->assign('res',$res);
        $this->assign('user_id',$user_id);
        return $this->fetch();
    }
    
    //结构列表
    public function StructureList()
    {
        $res = db('wx_jg_structure')->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //新增结构
    public function StructureAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['structure_name'] = $post['structure_name'];
            $data['create_time'] = time();
            $res = db('wx_jg_structure')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        return $this->fetch();
    }
    
    //结构编辑
    public function StructureEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['structure_name'] = $post['structure_name'];
            $data['create_time'] = time();
            $res = db('wx_jg_structure')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_structure')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        
        return $this->fetch();
    }
    
    //结构删除
    public function StructureDel($id)
    {
        $res = db('wx_jg_structure')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/StructureList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/StructureList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //建造记录
    public function framed_Data()
    {   
        $project_id = input('id');
        $this->assign('id', $project_id);
        $seek['seek'] = input('seek')?input('seek'):'';
        
        #参数错误
        if (empty($project_id)){
            $data = [];
            $data['status'] = 0;
            $data['info'] = '参数错误';
            $data['url'] = url('admin/Hunanconstr/project_list');
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
        
        $res = db('wx_jg_record')
                ->where("item_id='{$project_id}'")
                ->where("ban_number LIKE '%{$seek['seek']}%'")
                ->order('ban_number asc')
                ->order('create_time asc')
                ->paginate($this->page,false,array('query' => ['seek'=>$seek['seek'],'id'=>$project_id]));
                
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //编辑建造记录
    public function framedData_Edit(Request $request)
    {
        $sk = $this->accountId;
        $project_id = input('project_id');
        $this->assign('project_id', $project_id);
        
        #参数错误
        if (empty($project_id)){
            $data = [];
            $data['status'] = 0;
            $data['info'] = '参数错误';
            $data['url'] = url('admin/Hunanconstr/project_list');
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
        
        if (request()->isPost()) {
            $post = input('post.');
        
            #多图
            $mg=$this->List_img_uploads('image',$sk,$request);
            $post['image'] = $mg;
            if (!empty($mg)){
                $id = input('id');
                $images = db('wx_jg_record')->where("id='{$id}'")->value('image');
                $resolve_imgs = $this->resolve_imgs($images,$mg);
                $data['image'] = $resolve_imgs;
            }
            
            $id = input('id');
            $res = db('wx_jg_record')->where("id='{$id}'")->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/framed_Data',['id'=>$project_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/framed_Data',['id'=>$project_id]);
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }else{
            #获取店铺信息
            $id=input('id');
            $where['id'] = $id;
            $where['item_id'] = $project_id;
            $res = db('wx_jg_record')->where($where)->find();
            $this->assign('res', $res);
            
            #轮播图
            $imges = $res['image'];
            $imges1 = '';
            $imges2 = '';
            $imges3 = '';
            if (!empty($imges)) {
                //转换格式
                $imges = json_decode($imges, true);
                foreach ($imges as $k => $v) {
                    if (isset($imges[0])) {
                        $imges1 = $imges[0];
                    }else{
                        $imges1 = '';
                    }
                    if (isset($imges[1])) {
                        $imges2 = $imges[1];
                    }else{
                        $imges2 = '';
                    }
                    if (isset($imges[2])) {
                        $imges3 = $imges[2];
                    }else{
                        $imges3 = '';
                    }
        
                }
            }
            $this->assign('imges1', $imges1);
            $this->assign('imges2', $imges2);
            $this->assign('imges3', $imges3);
        
            return $this->fetch();
        }
    }
    
    //大事记
    public function memorabilia($id)
    {
        $project_id = input('id');
        $this->assign('id', $project_id);
        
        #参数错误
        if (empty($project_id)){
            $data = [];
            $data['status'] = 0;
            $data['info'] = '参数错误';
            $data['url'] = url('admin/Hunanconstr/project_list');
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
        
        $res = db('wx_jg_event')
        ->where("item_id='{$project_id}'")
        ->order('sort asc')
        ->order('create_time asc')
        ->paginate($this->page,false,array('query' => ['id'=>$project_id]));
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        $this->assign('cid',$id);
        return $this->fetch();
    }
    
    //大事记图片审核
    public function thumb_examine($id)
    {
        $res = db('wx_jg_event')->where(['id'=>$id])->update(['state'=>1]);
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //大事记图片修改
    public function thumb_de($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //图片上传
            if (isset($_FILES['image'])&&!empty($_FILES['image'])) {
                $file = request()->file('image');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $data['event_img'] = $thumb;
                }
            }
            $res = db('wx_jg_event')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                echo $thumb;
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
       
    }
    
    //云相册列表
    public function CloudAlbum($id)
    {
        $res = db('wx_jg_album')->where(['item_id'=>$id])->order('time_new desc')->paginate($this->page,false);
        foreach ($res as $k => $v){
            $v['image'] = json_decode($v['image']);
            $res -> offsetSet($k,$v);
        }
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //云相册编辑
    public function CloudAlbumEdit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            dump($post['id']);die;
        }
        $res = db('wx_jg_album')->where(['id'=>$id])->find();
        $res['image'] = json_decode($res['image']);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //云相册删除
    public function CloudAlbumDel($id)
    {
        $res = db('wx_jg_album')->where(['id' => $id])->delete();
        if ($res) {
            //删除圈圈照片
            db('wx_jg_coworker')->where(['type'=>1,'type_id'=>$id])->delete();
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }

    /**
     * 刷刷管理
     */
    //公告列表
    public function NoticeList()
    {
        $res = db('wx_jg_article')->order('create_time desc')->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //公告增加
    public function NoticeAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['countet'] = $post['countet'];
            $data['create_time'] = time();
            $res = db('wx_jg_article')->insert($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        return $this->fetch();
    }
    
    //公告修改
    public function NoticeEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['countet'] = $post['countet'];
            $data['create_time'] = time();
            $res = db('wx_jg_article')->where(['id'=>$id])->update($data);
        
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_article')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //公告删除
    public function NoticeDel($id)
    {
        $res = db('wx_jg_article')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/NoticeList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/NoticeList';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //刷刷图片管理
    public function ThumbList()
    {
        $res = db('wx_jg_coworker')->field('image')->select();

        $image = [];
        foreach ($res as $k => $v){
                $image[] = json_decode($v['image']);

        }
        $img = [];
        foreach ($image as $k => $v){
            foreach ($v as $key => $val){
                $img[] = $val;
            }
            
        }
        $this->assign('res',$res);
        $this->assign('img',$img);
        return $this->fetch();
    }
    
    //周报
    public function Weekly()
    {
        if (request()->isPost()) {
            $post = request()->only(['day', 'city','county', 'resettlement'], 'post');
            $data['day'] = $post['day'];
            $data['city'] = $post['city'];
            $data['county'] = $post['county'];
            $data['resettlement'] = $post['resettlement'];
            $res = db('wx_jg_weekly')->where("id != ''")->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Weekly';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Weekly';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_weekly')->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
        
    /**
     * 使用说明
     */
    //说明内容
    public function Content()
    {
        $res = db('wx_jg_explain_content')->where("type = 1")->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //说明编辑
    public function ContentEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['title1'] = $post['title1'];
            $data['title2'] = $post['title2'];
            $data['url'] = $post['url'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_content')->where(['id'=>$id])->update($data);
        
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Content';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Content';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_explain_content')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //联系人
    public function Contactslist()
    {
        $res = db('wx_jg_explain_contacts')->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //新增联系人
    public function ContactsAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            $data['weixin'] = $post['weixin'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_contacts')->insert($data);
        
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        return $this->fetch();
    }
    
    //编辑联系人
    public function ContactsEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            $data['weixin'] = $post['weixin'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_contacts')->where(['id'=>$id])->update($data);
        
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_explain_contacts')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //删除联系人
    public function ContactsDel($id)
    {
        $res = db('wx_jg_explain_contacts')->where("id = $id")->delete();
        
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/Contactslist';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/Contactslist';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    //首页公告
    public function Notice()
    {
        if (request()->isPost()) {
            $post = request()->only(['title', 'notice_on','delay_on'], 'post');
            $data['title'] = $post['title'];
            $data['notice_on'] = $post['notice_on'];
            $data['delay_on'] = $post['delay_on'];
            $res = db('wx_jg_explain_content')->where("type = 2")->update($data);
        
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Notice';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Notice';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
        $res = db('wx_jg_explain_content')->where("type = 2")->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    /**
     * 系统设置
     */
    public function Binding()
    {
        $res = db('wx_jg_code')->select();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    
    //项目模型图片编辑
    public function thumb_model($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //图片上传
            if (isset($_FILES['image'])&&!empty($_FILES['image'])) {
                $file = request()->file('image');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $thumb = 'http://bike.wei-yong.com/'.'uploads' . '/' . $info->getSaveName();
                    $data['project_model'] = $thumb;
                }
            }
            $res = db('project')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                echo $thumb;
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data',$data);
                return $this->fetch('statuspage');
            }
        }
    }
    
    //解除财务
    public function Finance($id)
    {
        //获取公司code
        $company = db('company')->field('code')->where("id = $id")->find();
        //删除绑定人
        $res = db('wx_jg_binding')->where(['type' => 2,'code' => $company['code']])->delete();
        
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/company_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/company_list';
            $this->assign('data',$data);
            return $this->fetch('statuspage');
        }
    }
    
    
    
    
    
}